﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using Entity;
using DataControls;

namespace GiaoVien_QuanLy_DeTai
{
    public partial class LUONGKN_Form : Form
    {
        GIANGDAY_Data giangday_Data = new GIANGDAY_Data();
        LUONGKN_Data luongkn_Data = new LUONGKN_Data();
        THAMSO thamso = new THAMSO();
        LUONGKN luongkn = new LUONGKN();
        NX_DETAI_Data nx_detai_Data = new NX_DETAI_Data();
        DETAI_Data detai_Data = new DETAI_Data();
        GIAOVIEN_Data giaovien_Data = new GIAOVIEN_Data();
        public LUONGKN_Form()
        {
            InitializeComponent();
        }

        private void LUONGKN_Form_Load(object sender, EventArgs e)
        {
            
            cbGiaoVien.Items.Clear();
            giaovien_Data.giaovien_Combobox(cbGiaoVien);

            txtMaLuongKN.Text = "LKN";
            txtNamHoc.Text = DateTime.Now.Year.ToString();
            txtTongTietHuongDan.Text = "";
            txtTongTietNhanXet.Text = "";
            txtLuongKiemNghiem.Text = "";
        }

        private void btnHienThi_Click(object sender, EventArgs e)
        {           
            luongkn.hienthi_LuongKN(dtgLuongKN);

            cbGiaoVien.Items.Clear();
            giaovien_Data.giaovien_Combobox(cbGiaoVien);

            txtMaLuongKN.Text = "LKN";
            txtNamHoc.Text = DateTime.Now.Year.ToString();
            txtTongTietHuongDan.Text = "";
            txtTongTietNhanXet.Text = "";
            txtLuongKiemNghiem.Text = "";
        }

        private void btnThem_Click(object sender, EventArgs e)
        {
            if (txtMaLuongKN.Text == "")
            {
                MessageBox.Show("Ban chua nhap Ma Luong ");
                txtMaLuongKN.Focus();
                return;
            }
            else if (luongkn_Data.kiemtra_MaLuongKN(txtMaLuongKN.Text.ToString()))
            {
                MessageBox.Show("Ban nhap trung ma");
                txtMaLuongKN.Focus();
                return;
            }
            else if (txtNamHoc.Text == "")
            {
                MessageBox.Show("Ban chua nhap nam hoc");
                txtNamHoc.Focus();
                return;
            }
            else if (cbGiaoVien.Text == "")
            {
                MessageBox.Show("ban chua chon giao vien");
                return;
            }
            else if (txtTongTietHuongDan.Text == "")
            {
                MessageBox.Show("Giao vien chua co so tiet huong dan");
                txtTongTietHuongDan.Focus();
                return;
            }
            else if (txtTongTietNhanXet.Text == "")
            {
                MessageBox.Show("Giao vien chua co so tiet giang day");
                txtTongTietNhanXet.Focus();
                return;
            }
            else if (txtMaLuongKN.Text == "")
            {
                MessageBox.Show("Giao vien chua co so luong");
                return;
            }
            else
            {
                try
                {
                    if ((luongkn.namhoc = Convert.ToInt16(txtNamHoc.Text)) < 1990 || (luongkn.namhoc = Convert.ToInt16(txtNamHoc.Text)) > 2011)
                    {
                        MessageBox.Show("Ban nhap sai nam hoc");
                        txtNamHoc.Focus();
                        return;
                    }
                }
                catch (Exception ex)
                {
                    MessageBox.Show("Ban nhap sai du lieu nam hoc \n" + ex.Message);
                    txtNamHoc.Focus();
                    return;
                }
                luongkn.maluongkn = txtMaLuongKN.Text.ToString();
                luongkn.magv = ((ListItem)(cbGiaoVien.SelectedItem)).value;
                luongkn.hocki = (int)nmHocKy.Value;
                luongkn.luongkn = (float)Convert.ToDouble(txtLuongKiemNghiem.Text);
                luongkn.tongtiethuongdan = (float)Convert.ToDouble(txtTongTietHuongDan.Text);
                luongkn.tongtietnhanxet = (float)Convert.ToDouble(txtTongTietNhanXet.Text);

                luongkn.them_LuongKN(luongkn);
            }


            luongkn.hienthi_LuongKN(dtgLuongKN);

            cbGiaoVien.Items.Clear();
            giaovien_Data.giaovien_Combobox(cbGiaoVien);

            txtMaLuongKN.Text = "LKN";
            txtNamHoc.Text = DateTime.Now.Year.ToString();
            txtTongTietHuongDan.Text = "";
            txtTongTietNhanXet.Text = "";
            txtLuongKiemNghiem.Text = "";
        }

        private void btnSua_Click(object sender, EventArgs e)
        {
            if (txtMaLuongKN.Text == "")
            {
                MessageBox.Show("Ban chua nhap Ma Luong ");
                txtMaLuongKN.Focus();
                return;
            }

            else if (txtNamHoc.Text == "")
            {
                MessageBox.Show("Ban chua nhap nam hoc");
                txtNamHoc.Focus();
                return;
            }
            else if (cbGiaoVien.Text == "")
            {
                MessageBox.Show("ban chua chon giao vien");
                return;
            }
            else if (txtTongTietHuongDan.Text == "")
            {
                MessageBox.Show("Giao vien chua co so tiet huong dan");
                txtTongTietHuongDan.Focus();
                return;
            }
            else if (txtTongTietNhanXet.Text == "")
            {
                MessageBox.Show("Giao vien chua co so tiet giang day");
                txtTongTietNhanXet.Focus();
                return;
            }
            else if (txtMaLuongKN.Text == "")
            {
                MessageBox.Show("Giao vien chua co so luong");
                return;
            }
            else
            {
                try
                {
                    if ((luongkn.namhoc = Convert.ToInt16(txtNamHoc.Text)) < 1990 || (luongkn.namhoc = Convert.ToInt16(txtNamHoc.Text)) > 2011)
                    {
                        MessageBox.Show("Ban nhap sai nam hoc");
                        txtNamHoc.Focus();
                        return;
                    }
                }
                catch (Exception ex)
                {
                    MessageBox.Show("Ban nhap sai du lieu nam hoc\n" +ex.Message);
                    txtNamHoc.Focus();
                    return;
                }
                luongkn.maluongkn = txtMaLuongKN.Text.ToString();
                luongkn.magv = ((ListItem)(cbGiaoVien.SelectedItem)).value;
                luongkn.hocki = (int)nmHocKy.Value;
                luongkn.luongkn = (float)Convert.ToDouble(txtLuongKiemNghiem.Text);
                luongkn.tongtiethuongdan = (float)Convert.ToDouble(txtTongTietHuongDan.Text);
                luongkn.tongtietnhanxet = (float)Convert.ToDouble(txtTongTietNhanXet.Text);

                luongkn.sua_LuongKN(luongkn);
            }
            luongkn.hienthi_LuongKN(dtgLuongKN);

            cbGiaoVien.Items.Clear();
            giaovien_Data.giaovien_Combobox(cbGiaoVien);

            txtMaLuongKN.Text = "LKN";
            txtNamHoc.Text = DateTime.Now.Year.ToString();
            txtTongTietHuongDan.Text = "";
            txtTongTietNhanXet.Text = "";
            txtLuongKiemNghiem.Text = "";
        }

        private void btnXoa_Click(object sender, EventArgs e)
        {
            if (txtMaLuongKN.Text == "")
            {
                MessageBox.Show("Ban chua nhap Ma Luong ");
                txtMaLuongKN.Focus();
                return;
            }
            else
            {
                luongkn.xoa_LuongKN(txtMaLuongKN.Text.ToString());
            }

            luongkn.hienthi_LuongKN(dtgLuongKN);

            cbGiaoVien.Items.Clear();
            giaovien_Data.giaovien_Combobox(cbGiaoVien);

            txtMaLuongKN.Text = "LKN";
            txtNamHoc.Text = DateTime.Now.Year.ToString();
            txtTongTietHuongDan.Text = "";
            txtTongTietNhanXet.Text = "";
            txtLuongKiemNghiem.Text = "";
        }

        private void cbGiaoVien_SelectedIndexChanged(object sender, EventArgs e)
        {
            int nh = 0;
            int hk = 0;
            String magiaovien = ((ListItem)(cbGiaoVien.SelectedItem)).value;
            try
            {
                if ((nh = Convert.ToInt16(txtNamHoc.Text)) <= 0 || (nh = Convert.ToInt16(txtNamHoc.Text)) > DateTime.Now.Year)
                {
                    MessageBox.Show("ban nhap sai nam hoc");
                    txtNamHoc.Focus();
                    return;
                }
                else if ((hk = (int)nmHocKy.Value) <= 0 || (hk = (int)nmHocKy.Value) >= 20)
                {
                    MessageBox.Show("ban nhap sai hoc ki");
                    txtNamHoc.Focus();
                    return;
                }
            }
            catch (Exception ex)
            {
                MessageBox.Show("Ban nhap kieu du lieu nam hoc \n"+ ex.Message);
            }
            nx_detai_Data.hienthi_DeTaitheoGV(dtgNhanXet, magiaovien);
            detai_Data.hienthi_DeTaitheoGV(dtgHuongDan, magiaovien);

            int sotietHuongDan = 0;
            sotietHuongDan =  detai_Data.tongtiet_HuongDan(magiaovien,hk,nh);
            txtTongTietHuongDan.Text = sotietHuongDan.ToString();

            int sotietNhanXet = 0;
            sotietNhanXet = nx_detai_Data.tongtiet_Nhanxet(magiaovien, hk, nh);
            txtTongTietNhanXet.Text = sotietNhanXet.ToString();

            float tongtien = 0;
            float tienluongchuan = thamso.tien_TietChuan();
            tongtien = (sotietHuongDan + sotietNhanXet) * tienluongchuan;
            txtLuongKiemNghiem.Text = ((double)tongtien).ToString();
        }

        private void dtgLuongKN_RowEnter(object sender, DataGridViewCellEventArgs e)
        {
            try
            {
                dtgLuongKN.AllowUserToAddRows = false;
                int dong = e.RowIndex;
                txtMaLuongKN.Text = dtgLuongKN.Rows[dong].Cells[0].Value.ToString();
                txtNamHoc.Text = dtgLuongKN.Rows[dong].Cells[3].Value.ToString();
                txtTongTietHuongDan.Text = dtgLuongKN.Rows[dong].Cells[4].Value.ToString();
                txtTongTietNhanXet.Text = dtgLuongKN.Rows[dong].Cells[5].Value.ToString();
                txtLuongKiemNghiem.Text = dtgLuongKN.Rows[dong].Cells[6].Value.ToString();
                nmHocKy.Value = (int)dtgLuongKN.Rows[dong].Cells[2].Value;

                String magiaovien = dtgLuongKN.Rows[dong].Cells[1].Value.ToString();
                giangday_Data.layten_GiaoVien(magiaovien, cbGiaoVien);
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message);
                return;
            }
        }

       

        
        
    }
}
